<?php
/* 
 *发货管理
 */
class ShippingAction extends TransAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
            $where['logi_no'] = array('like',"%".$key."%");
            $where['Shipping.mano'] = array('like',"%".$key."%");
            $where['Product.name'] = array('like',"%".$key."%");
            $where['Product.ename'] = array('like',"%".$key."%");
            $where['Product.cas'] = array('like',"%".$key."%");
            $where['_logic'] = "or";
            $map['_complex'] = $where;
        }
        if( $_REQUEST['status'] != '' )
            $map['Shipping.status'] = $_REQUEST['status'];
        if(!empty($_REQUEST['customer']))
            $map['_string'] = "(Customer.name like '%".$_REQUEST['customer']."%') or (Customer.company like '%".$_REQUEST['customer']."%')";

        //time 
        $cond_time = array();
        if( $_REQUEST['start_time'] != '' )
          array_push($cond_time, array( 'egt' , strtotime($_REQUEST['start_time']) ) );
          //$cond_time = array( 'egt' , strtotime($_REQUEST['start_time']) );
        if( $_REQUEST['end_time'] != '' )
          array_push($cond_time, array( 'elt' , strtotime($_REQUEST['end_time']) ) );
	if(count($cond_time) >0 ){
  	    $map['create_time'] = $cond_time;	
	}
        
    }

    public function index()
    {//列表过滤器，生成查询Map对象
        $map = $this->_search ();
        if (method_exists ( $this, '_filter' )) {
            $this->_filter ( $map );
        }
        
        $model = D ('ShippingOrderDetailView');
        if (! empty ( $model )) {
            $this->_list ( $model, $map,'',false,true );//group 去掉重复数据
        }
        $this->display ();
        return;
    }

    //待发货列表
    public function toshipping(){
        $map['Order_detail.status'] = array('between',array('0','4'));    
        $model = D('OrderView');
        $this->_list ( $model, $map );
        $this->display ();
        return;
    }

    public function update(){
        //计算运费管理
        if($_POST['real_logifee'] != "" ) //实际单位运费
            $_POST['real_unit_logifee'] = $_POST['real_logifee'] / getModelName($_POST['id'],'Shipping','net_weight');
        if( !empty($_POST['real_logifee']) && !empty($_POST['rmb_logifee'])  )
            $_POST['logifee_diff'] = $_POST['rmb_logifee'] - $_POST['real_logifee']; //运费差额
        //如果发货单状态是客户签收或问题件，则运费管理状态为待结算
        if($_POST['status'] == 4 || $_POST['status'] == -4 )
            $_POST['logifee_status'] = 1; 
        
        $sdetail = M('Shipping_detail')->where('shipping_id = '.$_POST['id'])->findAll();
        
        foreach ($sdetail as $key => $value) {
            $pid = $value['product_id'];
            $specification = getModelName('Order_detail',$value['order_detail_id']);
            $number = $value['number'];
            if($_POST['status'] == 3) //发货时，减去所有库存
                A('Trans')->updateStock($pid,$specification,'out', $number);
            if($_POST['status'] == 1 ) //5=>'打包完成，单据待制作',
                M('Order_detail')->where('id = '.$value['order_detail_id'])->setField('status',5);
            if($_POST['status'] == 3 ) //6=>'单据制作完成，已发货',
                M('Order_detail')->where('id = '.$value['order_detail_id'])->setField('status',6);
            if($_POST['status'] == 4 ) //发货单客户签收，则小订单也是签收 7
                M('Order_detail')->where('id = '.$value['order_detail_id'])->setField('status',7);
            if($_POST['status'] < 0 ) //发货单异常，则小订单状态是：问题件 = -3
                M('Order_detail')->where('id = '.$value['order_detail_id'])->setField('status',-3);
        }
        
        parent::update();
    }

    //某个待发货的小订单详情
    public function odetail(){
        $model = M ( 'Order_detail' );
        $id = $_REQUEST ['id'];
        $vo = $model->getById ( $id );
        $this->assign ( 'vo', $vo );

        $stocks = M('Stock')->where('product_id ='.$vo['product_id'])->findAll();
        $this->assign('stocks',$stocks);
        $this->display ();
    }

    //打包操作，状态必须是“仓库操作中”且是同一个客户的订单，才能打包
    public function doshipping(){
        $model = M ( 'Order_detail');
        $id = $_REQUEST [$model->getPk ()];
        if(isset($id)){
            $condition = array (
                'id' => array ('in', explode ( ',', $id ) ),
                'status' =>4 ,//仓库操作中
             );
            $vo = $model->where($condition)->findAll();
            if(!is_array($vo))
                $this->error('请选择状态为 仓库操作中 的销售订单进行打包！');
            $cid1 = 0;  //customer_id
            foreach ($vo as $key => $value) {
                if($key == 0)
                    $cid1 = getModelName($value['order_id'],'Order','customer_id');
                else{
                    if($cid1 == getModelName($value['order_id'],'Order','customer_id'))
                        continue;
                    else
                        $this->error('请选择同一个客户的销售订单进行打包！');
                }

            }
            $this->assign ( 'vo', $vo );
            $this->display ();
        }else{
            $this->error('请选择待发货的销售单号！');
        }
        
    }

     //相关产品
    public function detail($value='')
    {

        $map = 'shipping_id = '.intval($_REQUEST['id']) ;
        $count = M('Shipping_detail')->where($map)->count();
        if($count > 0){
            import ( "@.ORG.Page" );
            $page = new Page($count,25);
            $ps = M('Shipping_detail')->where($map)->limit($page->firstRow.','.$page->listRows)->order('id desc')->findAll(); 
            $assign_data = array(
                'products' => $ps,
                'page' => $page->show(),
            ); 
            $this->assign($assign_data);
        }
        $this->display();
    }

    //打印出库单
    public function print_export(){
        $vo = M('Shipping')->getById ( $_GET['id'] );
        $this->assign ( 'vo', $vo );
        $this->display ();
    }

    //增补新订单
    public function insert_new_order(){
        $shipping_id = $_GET['shipping_id'];
        $order_detail_id = $_GET['order_detail_id'];
        $odetail = M('Order_detail')->getById($order_detail_id);//order_detail
        $data = array(
            'shipping_id' => $shipping_id,
            'order_detail_id' => $order_detail_id,
            'price' => $odetail['price'],
            'product_id' => $odetail['product_id'],
            'number' => $odetail['number'],
            'total' => $odetail['total'],
            'logifee' => $odetail['logifee'],
            'sum_total' => ($odetail['logifee'] + $odetail['total']),
        );
        if($rs = M('Shipping_detail')->where('order_detail_id ='.$order_detail_id.' and shipping_id='.$shipping_id )->getField('id')  )
            $this->error("发货单中已经添加了该订单！请勿重复添加");
        $sid = M('Shipping_detail')->add($data);
        if($sid){
            //更新对应销售分单的状态
            $shipping_status = M('Shipping')->where('id = '.$shipping_id)->getField('status');
            $new_order_detail_status = 0;
            switch($shipping_status){
               case 1:
                    $new_order_detail_status = 5;
                    break;
               case 3:
                    $new_order_detail_status = 6;
                    break;
                case 4:
                    $new_order_detail_status = 7;
                    break;
               default:
                    $new_order_detail_status = -3;
            }
                M('Order_detail')->where('id = '.$order_detail_id)->setField('status',$new_order_detail_status);

            //更新发货单的总金额
            M('Shipping')->where('id = '.$shipping_id)->setInc('total', $odetail['total'] );
            $this->assign('jumpUrl',__APP__.'/Shipping/edit/id/'.$shipping_id);
            $this->error("增补订单成功！");    
        }else
            $this->error("增补订单失败！");

    }

}
?>
